package nina.yiyou.performance.s11collect.input

import groovy.transform.PackageScope
import nina.yiyou.salary.input.util.CellValueGetter
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet

import java.util.stream.Collectors

@PackageScope
class SelfEvalExtractor {

  SelfEvalExtractor(Sheet sheet, String fillerId, String fillName) {
    _sheet = sheet
    _fillerId = fillerId
    _fillName = fillName
  }

  List<CollectFilledReader.Eval> extract() {
    List<Row> dataRows = (1..18).collect { _sheet.getRow(it) }
    Map groupCol = new GroupColumnGetter(dataRows).getColumn()

    List valueCol = dataRows.stream()
        .map { it.getCell(it.rowNum != dataRows[-1].rowNum ? 7 : 3) }
        .map { new CellValueGetter(it).get() }
        .collect(Collectors.toList())

    def toEvalItem = { int row ->
      new EvalItemMaker(_fillerId, _fillName,
          _fillerId, _fillName, groupCol, valueCol, row).make()
    }

    return (1..<valueCol.size()).stream()
        .map { toEvalItem.call(it) }
        .collect(Collectors.toList())
  }

  private final Sheet _sheet

  private final String _fillerId
  private final String _fillName
}
